/** @file
* General purpose TCP client interface.
*/
#ifndef __SOCKET_CLIENT__
#define __SOCKET_CLIENT__

#ifdef _WIN32
#pragma comment(lib, "wininet.lib")
#include <ws2tcpip.h>
#include <windows.h>
#include <winsock2.h>		// Windows socket API

#else //Linux

#include <unistd.h>		// Sleep function
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>	// TCP protocol library
#include <arpa/inet.h>		// IP Address conversion library

#define closesocket(x) close(x)
#define strwinerr(x) std::strerror(x)
#define WSAGetLastError() errno

#endif

/** This class manages TCP/IP interface. */
class tcp_client
{
  /** file identifier for the connected tcp socket */
  size_t tcp_socket;

  /** return values of winsock functions */
  size_t ret_val;

  /** structure of Winsock TCP settings */
  struct sockaddr_in serv_addr;

public:

  /** Constructor.
   * @param serv_ip Device IP address
   * @param serv_port TCP port number
   */
    tcp_client (char serv_ip[], unsigned serv_port);
   ~tcp_client ();		//!< Destructor
  void tcp_connect ();

  /** Read message from socket.
  * Message is read with defined length
  * @retval buffer A byte array of received data
  * @param count Number of bytes expected to be received
  */
  void tcp_read (char buffer[], unsigned long count);

  /** Send message from socket
  * @param send_code code to be sent to device, must end with \\0
  */
  void tcp_send (char send_code[]);

  /** Close the socket */
  void tcp_disconnect ();

  /** Check and print error in socket.
  * If the return value is -1, the program print the error message to stdout, and
  * terminate the program with value -1. Otherwise, print message @c SUCCESS to
  * stdout.
  */
  void chk_error ();
};


#endif
